package com.tbit.config.mvc;

import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.tbit.config.mvc.converter.StringToTagBindStatusConverter;
import com.tbit.config.mvc.converter.StringToTagStatusConverter;
import com.tbit.mvc.convert.StringRemoveBlankConvert;
import com.tbit.mvc.convert.StringToDateConvert;
import com.tbit.mvc.handler.MyAcceptHeaderLocaleResolver;
import com.tbit.uqbike.enums.TagBindStatus;
import com.tbit.uqbike.serialize.TagBindStatusDeserializer;
import com.tbit.uqbike.webmanager.controller.interceptor.LanguageInterceptor;
import com.tbit.uqbike.webmanager.controller.interceptor.LoginInterceptor;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenzhuang
 * @Description:
 * @date 2022/7/29 17:45
 */
@Configuration
public class MvcConfig implements WebMvcConfigurer {

    /**
     * 注册自定义的LocaleResolver
     **/
    @Bean
    public LocaleResolver localeResolver() {
        return new MyAcceptHeaderLocaleResolver();
    }

    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();


        // 注册自定义的反序列化器
        ParserConfig parserConfig = fastJsonConfig.getParserConfig();
        parserConfig.putDeserializer(TagBindStatus.class, new TagBindStatusDeserializer());


//        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//        // Date格式设置
//        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        fastJsonConverter.setFastJsonConfig(fastJsonConfig);

        //3、在convert中添加配置信息
        //处理中文乱码问题
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastJsonConverter.setSupportedMediaTypes(fastMediaTypes);
        HttpMessageConverter<?> converter = fastJsonConverter;
        return new HttpMessageConverters(converter);
    }


    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new StringRemoveBlankConvert());
        registry.addConverter(new StringToDateConvert());
        registry.addConverter(new StringToTagBindStatusConverter());
        registry.addConverter(new StringToTagStatusConverter());
        WebMvcConfigurer.super.addFormatters(registry);
    }

    @Bean
    public LanguageInterceptor languageInterceptor() {
        return new LanguageInterceptor();
    }


    @Bean
    public LoginInterceptor loginInterceptor() {
        return new LoginInterceptor();
    }

    /**
     * 添加拦截器
     **/
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(languageInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/sugar/**");

        registry.addInterceptor(loginInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/error")
                .excludePathPatterns("/sugar/**")
                .excludePathPatterns("/code/sendCode.do")
                .excludePathPatterns("/account/loginWithCode.do")
                .excludePathPatterns("/account/loginWithPassword.do")
                .excludePathPatterns("/account/login.do")
                .excludePathPatterns("/account/loginWithSystem.do")
                .excludePathPatterns("/account/heartbeat.do")
                .excludePathPatterns("/account/getBrandMsg.do")
                .excludePathPatterns("/machine/exportMachineInfoTemplate.do")
                .excludePathPatterns("/machineBorrow/finishOrderM.do")
                .excludePathPatterns("/machine/exportModMachineInfoTemplate.do")
                .excludePathPatterns("/advert/getImage.do")
                .excludePathPatterns("/fee/getMachineFee.do")
                .excludePathPatterns("/advert/getImage.do")
                .excludePathPatterns("/account/getBrandConfigByHost.do")
                .excludePathPatterns("/account/getBrandConfigByName.do")
                .excludePathPatterns("/common/**")
                .excludePathPatterns("/common/*")
                .excludePathPatterns("/heartbeat.do")
                .excludePathPatterns("/accountDayStat/operationalDataToMain.do")
                .excludePathPatterns("/ruleRideLog/**")
                .excludePathPatterns("/stat/getFineMoney.do")
                .excludePathPatterns("/rideLogDetails/getByOrderId.do")
                .excludePathPatterns("/orderAppeal/getresult.do")
                .excludePathPatterns("/refundOperation/rpc/rideOrderRefund.do")
                .excludePathPatterns("/rideLogDetails/rpc/editOrder.do")
                .excludePathPatterns("/transactionOrderStat/getIncomePageApp.do")
                .excludePathPatterns("/invoice/queryOrderHasBeenInvoicedByOrderNoApp.do")
                .excludePathPatterns("/refundOperation/depositRefundApp.do")
                .excludePathPatterns("/refundOperation/orderRefundApp.do")
                .excludePathPatterns("/auth/**")
        ;
    }

    /**
     * 严格按照*。do匹配 会影响‘项目内静态资源’的访问
     */
  /*  @Bean(name = "dispatcherServletRegistration")
    public DispatcherServletRegistrationBean dispatcherServletRegistration(DispatcherServlet dispatcherServlet,
                                                                           WebMvcProperties webMvcProperties, ObjectProvider<MultipartConfigElement> multipartConfig) {
        DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(dispatcherServlet,
                "*.do");
        registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
        registration.setLoadOnStartup(webMvcProperties.getServlet().getLoadOnStartup());
        multipartConfig.ifAvailable(registration::setMultipartConfig);
        return registration;
    }*/
}
